import * as types from './mutations_types'
import { getDepartmentAllListAPI } from '@/api/system/department'

export default {
  GetDepartmentList({ commit, state }, params = { request: true }) { // 获取部门列表-请求数据：默认必须走接口请求
    let { departmentList } = state
    let { request } = params
    return new Promise((resolve, reject) => {
      if (request || JSON.stringify(departmentList) === '[]') {
        getDepartmentAllListAPI({
          ...params
        }).then(response => {
          // let result = response.data
          let result = response
          // console.log(result)
          if (+result.code === 200) {
            commit(types.SET_DEPARTMENT_LIST, result.data || [])
            resolve(result)
          } else {
            reject(new Error(result.msg || '获取列表失败！'))
          }
        }).catch(error => {
          reject(error.message || '获取列表失败！')
        })
      } else {
        resolve({
          data: departmentList,
          msg: '交易成功',
          code: 200
        })
      }
    })
  }
}
